﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_SalaryPromotionBaseConfig : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IPositionTypeBLL ptBLL = new PositionTypeBLL();
     
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        private void bindClassCombo()
        {
            DataTable dt;
            SalaryPromotionBaseConfigBLL obj = new SalaryPromotionBaseConfigBLL();
            dt = obj.SelectComboClass(ddlPosType.SelectedValue.Length > 0 ? Int32.Parse(ddlPosType.SelectedValue): (int?)null );

            ComboClass.DataSource = dt;
            ComboClass.DataBind();
        }

        private void bindPosTypeCombo()
        { 
             IList<PositionTypeInfo> infoList = ptBLL.ListPositionTypes(1);
             ddlPosType.DataSource = infoList;
             ddlPosType.DataBind();
        }
        private void bindTxt(int sal_grp_seq)
        {
            SalaryPromotionBaseConfigBLL obj = new SalaryPromotionBaseConfigBLL(); 
            DataTable dt;
            dt = obj.BindText(sal_grp_seq);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    lblSalaryMin.Text = string.Format("{0:N0}", dt.Rows[0]["sal_min"]);
                    lblSalaryMax.Text = string.Format("{0:N0}", dt.Rows[0]["sal_max"]);
                    lblSalaryTmp.Text = string.Format("{0:N0}", dt.Rows[0]["sal_tmp"]);
                    lbl_grp_dtls_seq.Text = dt.Rows[0]["grp_dtls_seq"].ToString(); 
                }

                else
                {
                    lblSalaryMin.Text = "0";
                    lblSalaryMax.Text = "0";
                    lblSalaryTmp.Text = "0";
                }

            }
            else
            {
                lblSalaryMin.Text = "0";
                lblSalaryMax.Text = "0";
                lblSalaryTmp.Text = "0";
            }

            //txtLevelName.Focus();
            
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            //txtSalMin.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            //txtSalMax.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            //txtBase.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");

            if (!IsUserControlPostBack)
            {

                this.ViewState.Add("IsUserControlPostBack", true);            

            }
            if (DataItem != null)
            {
                bindPosTypeCombo();
                bindClassCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {

                    lblPrmBaseSeq.Text  = DataBinder.Eval(DataItem, "prm_base_seq").ToString();
                    txtBase.Text = DataBinder.Eval(DataItem, "sal_base").ToString();
                    txtSalMin.Text   = DataBinder.Eval(DataItem, "sal_min").ToString();
                    txtSalMax.Text   = DataBinder.Eval(DataItem, "sal_max").ToString();
                    txtLevelName.Text = DataBinder.Eval(DataItem, "sal_level_name").ToString();
                    ddlPosType.SelectedValue = DataBinder.Eval(DataItem, "pos_type_seq").ToString();
                    ComboClass.SelectedValue = DataBinder.Eval(DataItem, "sal_grp_seq").ToString();

                    int id;
                    id = int.Parse(ComboClass.SelectedValue);  
                    bindTxt(id); 
                }
            }
      
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            SalaryPromotionBaseConfigBLL obj = new SalaryPromotionBaseConfigBLL();

            int prm_seq = int.Parse(Session["prm_seq"].ToString() );   

            Double sal_base, sal_min, sal_max;
            //Double sal_extra_base, sal_extra_min, sal_extra_max;
            int sal_grp_seq = int.Parse(ComboClass.SelectedValue);
            string sal_level_name = txtLevelName.Text.Trim();

            sal_base = txtBase.Text.Length > 0 ? Convert.ToDouble(txtBase.Text) : 0;
            sal_min = txtSalMin.Text.Length > 0 ? Convert.ToDouble(txtSalMin.Text) : 0;
            sal_max = txtSalMax.Text.Length > 0 ? Convert.ToDouble(txtSalMax.Text) : 0;

            obj.Insert(prm_seq, sal_grp_seq, sal_level_name, sal_base, sal_min, sal_max
                , 0, 0, 0, ssMng.EmpSeqI);       
  
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SalaryPromotionBaseConfigBLL obj = new SalaryPromotionBaseConfigBLL();
            Double sal_base, sal_min, sal_max;
            //Double sal_extra_base, sal_extra_min, sal_extra_max;
            int sal_grp_seq = int.Parse(ComboClass.SelectedValue);
            string sal_level_name = txtLevelName.Text.Trim();

            sal_base = txtBase.Text.Length > 0 ? Convert.ToDouble(txtBase.Text) : 0;
            sal_min = txtSalMin.Text.Length > 0 ? Convert.ToDouble(txtSalMin.Text) : 0;
            sal_max = txtSalMax.Text.Length > 0 ? Convert.ToDouble(txtSalMax.Text) : 0;

            obj.Update(int.Parse(lblPrmBaseSeq.Text), sal_grp_seq, sal_level_name, sal_base, sal_min, sal_max
                , 0, 0, 0, ssMng.EmpSeqI);     

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void ComboClass_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            int id;
            id = int.Parse(ComboClass.SelectedValue);
            bindTxt(id); 
        }

        protected void ddlPosType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {            
            bindClassCombo();
            ComboClass.SelectedIndex = -1;
        }      
    
    }
}